package com.bfly.mall.product.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.product.dto.ProductCategoryFormDTO;
import com.bfly.mall.product.dto.ProductCategoryListDTO;
import com.bfly.mall.product.entity.ProductCategory;
import com.bfly.mall.product.service.IProductCategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * 商品类目
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 19:42
 */
@RestController
@RequestMapping(value = "/manage/category")
public class ProductCategoryController extends BaseController {

    @Autowired
    private IProductCategoryService categoryService;

    /**
     * 所有商品类目名称等信息树形结构
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/21 19:43
     */
    @GetMapping(value = "/all")
    @PermissionCheck(required = false)
    public void allCategoryTree(HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("parent_id", 0);
        List<ProductCategory> list = categoryService.getList(param);
        List<ProductCategoryListDTO> names = new ArrayList<>();
        for (ProductCategory productCategory : list) {
            ProductCategoryListDTO nameDTO = filterEmptyChildren(productCategory);
            names.add(nameDTO);
        }
        Collections.sort(names);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(names));
    }

    private ProductCategoryListDTO filterEmptyChildren(ProductCategory productCategory) {
        List<ProductCategory> list = productCategory.getChildren();
        List<ProductCategoryListDTO> names = null;
        if (CollectionUtils.isEmpty(list)) {
            productCategory.setChildren(null);
        } else {
            names = new ArrayList<>();
            for (ProductCategory c : list) {
                ProductCategoryListDTO childDTO = filterEmptyChildren(c);
                names.add(childDTO);
            }
            Collections.sort(names);
        }
        ProductCategoryListDTO dto = new ProductCategoryListDTO();
        BeanUtils.copyProperties(productCategory, dto);
        dto.setChildren(names);
        return dto;
    }

    /**
     * 添加产品类目
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/3 21:49
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "添加产品类目")
    public void addCategory(HttpServletResponse response, @Valid @RequestBody ProductCategoryFormDTO formDTO, BindingResult result) {
        validData(result);
        categoryService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑产品类目
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/3 21:56
     */
    @PostMapping(value = "/edit")
    @ActionModel(value = "编辑产品类目")
    public void editCategory(HttpServletResponse response, @Valid @RequestBody ProductCategoryFormDTO formDTO, BindingResult result) {
        validData(result);
        categoryService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除产品类目
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/3 21:56
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除产品类目")
    public void delCategory(HttpServletResponse response, @RequestBody Integer... ids) {
        categoryService.remove(ids);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}